package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.domain.CodeConfig;
import com.frcdi.qms.masterdata.service.ICodeConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 编码规则Controller
 *
 * @author cell
 * @date 2023-10-18
 */
@Api(tags = "编码规则")
@RestController
@RequestMapping("/masterData/codeConfig")
public class CodeConfigController extends BaseController
{
    @Autowired
    private ICodeConfigService codeConfigService;

    /**
     * 查询编码规则列表
     */
    @ApiOperation("查询编码规则列表")
    @PreAuthorize("@ss.hasPermi('masterData:codeConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(CodeConfig codeConfig)
    {
        startPage();
        List<CodeConfig> list = codeConfigService.selectCodeConfigList(codeConfig);
        return getDataTable(list);
    }

    /**
     * 导出编码规则列表
     */
    @ApiOperation("导出编码规则列表")
    @PreAuthorize("@ss.hasPermi('masterData:codeConfig:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CodeConfig codeConfig)
    {
        List<CodeConfig> list = codeConfigService.selectCodeConfigList(codeConfig);
        ExcelUtil<CodeConfig> util = new ExcelUtil<CodeConfig>(CodeConfig.class);
        util.exportExcel(response, list, "编码规则数据");
    }

    /**
     * 获取编码规则详细信息
     */
    @ApiOperation("获取编码规则详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:codeConfig:query')")
    @GetMapping(value = "/{codeId}")
    public AjaxResult getInfo(@PathVariable("codeId") Long codeId)
    {
        return success(codeConfigService.selectCodeConfigByCodeId(codeId));
    }

    /**
     * 新增编码规则
     */
    @ApiOperation("新增编码规则")
    @PreAuthorize("@ss.hasPermi('masterData:codeConfig:add')")
    @PostMapping
    public AjaxResult add(@RequestBody CodeConfig codeConfig)
    {
        return toAjax(codeConfigService.insertCodeConfig(codeConfig));
    }

    /**
     * 修改编码规则
     */
    @ApiOperation("修改编码规则")
    @PreAuthorize("@ss.hasPermi('masterData:codeConfig:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody CodeConfig codeConfig)
    {
        return toAjax(codeConfigService.updateCodeConfig(codeConfig));
    }
}
